<?php

/**
 * 
 * @author yuxun
 *
 */
class ServerCommand extends CConsoleCommand {

	//  */1 * * * * /usr/local/bin/php /data/web/webdata/oss/crontab.php server >> /home/webuser/tmp/server_clear.log
	/**
	 * 服务器定时清档(non-PHPdoc)
	 * @see CConsoleCommand::run()
	 */
    public function run($args) {
    	set_time_limit (0);
    	echo "............................... \n";
    	echo "server清档定时任务开始 \n";
    	echo "开始时间：".date('Y-m-d H:i:s',time())."\n";
 
        $nowTime = strtotime(date('Y-m-d H:i:00',time()));  //当前时间
        $modServer = new ModServer();
        //查询设置为当前时间清档的list
        $list = $modServer->findAllByAttributes(array('game'=>'mango','clear_time'=>$nowTime,'is_automatic'=>1));
        
        if(count($list) == 0){
        	echo "暂无符合的数据 \n";
        }

        foreach ($list as $key=>$val){
        	$exec_l = 'sudo /data/sh/web/wupdate_server_opentime.sh '.$val->develop_id.' '.$val->ip;
        	echo "sudo /data/sh/web/wupdate_server_opentime.sh ".$val->develop_id.' '.$val->ip."\n";
        	//$exec_l = 'whoami';
        	$output = '';
        	exec($exec_l,$output,$return_var);
        	$jb_out = '';
        	foreach ($output as $ok=>$ov){
        		$jb_out .= $ov.",\n";
        	}
            
        	$automatic_info = '';
        	if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态,修改为特殊状态
        		$automatic_info .= "序号：".$val->id." 的数据清档成功; "."\n";
        		$automatic_info .= "脚本输出内容：".$jb_out."\n";        		
        	}else{
        		$automatic_info .= "序号：".$val->id." 的数据执行清档脚本失败; "."\n<br/>";
        		$automatic_info .= "脚本输出内容：".$jb_out."\n";
        	}
        	echo $automatic_info;
        	$update = array();
        	$update['is_automatic'] = 2;
        	$update['automatic_info'] = $automatic_info;
        	$modServer->updateByPk($val->id, $update);
        }

        echo "结束时间：".date('Y-m-d H:i:s',time())."\n";
        echo "server清档定时任务结束 \n";
    }

   

        
}